﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for OutputXml
/// </summary>
public class OutputXml
{
    static public void RunExecutedProjectToXml( HttpResponse response, String projectFileName, Data.Data analysisProject )
    {
        response.Write( "<Project>" );

        if( projectFileName != null )
        {
            response.Write( "<ProjectFileName>" );
            response.Write( projectFileName ) ;
            response.Write( "</ProjectFileName>" );
        }            

        response.Write( "<Pages>" );
        for( int currentVisiblePageIndex = 0,  visiblePageCount = analysisProject.GetPageCount() ;
                currentVisiblePageIndex < visiblePageCount ;
                currentVisiblePageIndex++ )
        {
            analysisProject.SetSelectedPage( currentVisiblePageIndex );
            Data.Page currentPage = analysisProject.GetSelectedPage();
            if( currentPage.Visible() )
            {
                response.Write( "<Page>" );
                
                response.Write( "<PageCaption>");
                response.Write( currentPage.GetPageCaption() );
                response.Write( "</PageCaption>");
                
                response.Write( "<PageDescription>");
                response.Write( currentPage.GetPageDescription() );
                response.Write( "</PageDescription>" );
                
                GenXmlToPrintWriter( response, currentPage );
                
                response.Write( "</Page>" );
            }
        }
        response.Write( "</Pages>" );
        response.Write( "</Project>" );
    }
    
    static private void GenXmlToPrintWriter( HttpResponse response, Data.Page currentPage )
    {
        response.Write( "<Header>" );

        response.Write( "<RowLabelCaption>" );
        response.Write( currentPage.GetRowLabelName() );
        response.Write( "</RowLabelCaption>" );

        int   textCount = currentPage.GetTextColumnCount() ;
        if( textCount > 0 )
        {
            response.Write( "<TextColumnNames>"  );
            for( int textColumnIndex = 0;
                    textColumnIndex < textCount ; textColumnIndex++ )
            {
                response.Write( "<TextColumnName>"  );
                response.Write( currentPage.GetTextColumnName( textColumnIndex ) );
                response.Write( "</TextColumnName>" );
            }
            response.Write( "</TextColumnNames>" );
        }

        int columnCount = currentPage.GetNumericColumnCount() ;
        if( columnCount > 0 )
        {
            response.Write( "<NumericColumnNames>"  );
            for( int numericColumnIndex = 0;
                    numericColumnIndex < columnCount ; numericColumnIndex++ )
            {
                response.Write( "<NumericColumnName>"  );
                response.Write( currentPage.GetNumericColumnName(numericColumnIndex ) );
                response.Write( "</NumericColumnName>" );
            }
            response.Write( "</NumericColumnNames>" );
        }
        response.Write( "</Header>" );

        int rowCount = currentPage.GetRowCount() ;
        if( rowCount > 0 )
        {
            response.Write( "<Rows>" );
            for( int rowIndex = 0 ; rowIndex < rowCount ; rowIndex++ )
            {
                response.Write( "<Row>" );
                response.Write( "<RowLabelValue>" );
                response.Write( currentPage.GetRowLabelValue( rowIndex ) );
                response.Write( "</RowLabelValue>" );

                if( textCount > 0 )
                {
                    response.Write( "<TextColumnValues>"  );
                    for( int textColumnIndex = 0;
                            textColumnIndex < textCount ; textColumnIndex++ )
                    {
                        response.Write( "<TextColumnValue>"  );
                        response.Write( currentPage.GetTextColumnValue( rowIndex, textColumnIndex ) );
                        response.Write( "</TextColumnValue>"  );
                    }
                    response.Write( "</TextColumnValues>"  );
                }

                if( columnCount > 0 )
                {
                    response.Write( "<NumericColumnValues>"  );
                    for( int numericColumnIndex = 0;
                            numericColumnIndex < columnCount ; numericColumnIndex++ )
                    {
                        String alertLevel = currentPage.GetAlertLevel( rowIndex, numericColumnIndex );
                        response.Write( "<NumericColumnAlert>" ) ;
                        response.Write( alertLevel );
                        response.Write( "</NumericColumnAlert>" ) ;

                        response.Write( "<NumericColumnFormattedValue>"  );
                        response.Write( currentPage.GetNumericCellContents( rowIndex, numericColumnIndex, true ) );
                        response.Write( "</NumericColumnFormattedValue>"  );
                        
                        response.Write( "<NumericColumnFormattedValue>"  );
                        response.Write( currentPage.GetNumericCellContents( rowIndex, numericColumnIndex, false ) );
                        response.Write( "</NumericColumnFormattedValue>"  );
                    }
                    response.Write( "</NumericColumnValues>"  );
                }
                response.Write( "</Row>" );
            }
            response.Write( "</Rows>" );
        }
    }
}
